package me.zhengjie.minbearwcs.controller.common;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.minbearwcs.entity.DTO.response.Resp;
import me.zhengjie.minbearwcs.serivce.common.MinBearUploadService;
import me.zhengjie.minbearwcs.utils.ExcelReadUtil;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

@Slf4j
@RestController
@RequiredArgsConstructor
public class MinBearUploadController {

    private final MinBearUploadService minBearUploadService;


    @PostMapping("/upload/storage")
    public Resp upload(@RequestParam("file") MultipartFile file) throws IOException {
        Resp resp = Resp.build();
        resp.reason("文件上传成功");
        InputStream inputStream = file.getInputStream();
        try {
            Workbook workbook = ExcelReadUtil.getWorkbook(inputStream);
            minBearUploadService.importExcelData(workbook);

        } catch (Exception e) {
            log.error("文件上传失败", e);
            resp.reason("文件上传失败")
                    .code(false);
        } finally {
            inputStream.close();
        }


        return resp;
    }


    @GetMapping("/download/storage/template")
    public void out(HttpServletResponse response) throws IOException {
        minBearUploadService.exportStorageTemplate(response);
    }


    @GetMapping("/download/{logType}")
    public void outLog(HttpServletResponse response,@PathVariable String logType) throws IOException {
        minBearUploadService.exportLogModel(response,logType);
    }



    @PostMapping("/upload/java/package")
    public Resp uploadJavaPackage(@RequestParam("file") MultipartFile file) throws IOException {
        Resp resp = Resp.build();
        resp.reason("java package文件上传成功");
        minBearUploadService.uploadJavaPackage(file);


        return resp;
    }
}
